package com.ruoyi.college.controller;

import com.ruoyi.college.domain.College;
import com.ruoyi.college.domain.ExperimentClassProject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.college.domain.ExperimentProject;
import com.ruoyi.college.service.IExperimentProjectService;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 实验项目控制器
 * @author ruoyi
 * @date 2024-05-XX
 */
@Controller
@RequestMapping("/college/experimentProject")
public class ExperimentProjectController extends BaseController {
    @Autowired
    private IExperimentProjectService experimentProjectService;


    /** 页面前缀（对应resources/templates/college/目录） */
    private final String prefix = "college/experimentProject";

    /**
     * 跳转到实验项目列表页面
     * @param arrangementId 指定课程ID（从评分标准页面跳转携带）
     * @param mmap 传递参数到前端
     */
    @RequiresPermissions("college:experimentProject:view")
    @GetMapping("/list/{arrangementId}")
    public String list(@PathVariable("arrangementId") Integer arrangementId, ModelMap mmap) {
        // 传递课程安排ID到前端（后续添加/查询需用）
        mmap.put("arrangementId", arrangementId);
        return prefix + "/list";
    }

    /**
     * 异步获取实验项目列表数据（表格渲染用）
     * @param arrangementId 指定课程ID
     */
    @RequiresPermissions("college:experimentProject:view")
    @PostMapping("/listData")
    @ResponseBody
    public TableDataInfo listData(@RequestParam("arrangementId") Integer arrangementId) {
        startPage();
        List<ExperimentProject> list = experimentProjectService.getProjectListByArrangementId(arrangementId);
        return getDataTable(list);
    }

    //getProjectsByArrangementId
    //@RequiresPermissions("college:classExperimentProject:list")
    @GetMapping("/getProjectsByArrangementId")
    @ResponseBody
    public AjaxResult getProjectsByArrangementId(Integer arrangementId)
    {
        List<ExperimentProject> list = experimentProjectService.getProjectsByArrangementId(arrangementId);
        return AjaxResult.success(list);
    }

    /**
     * 跳转到添加实验项目页面
     * @param arrangementId 指定课程ID
     * @param mmap 传递参数
     */
    @RequiresPermissions("college:experimentProject:add")
    @GetMapping("/add/{arrangementId}")
    public String add(@PathVariable("arrangementId") Integer arrangementId, ModelMap mmap) {
        mmap.put("arrangementId", arrangementId);
        return prefix + "/add";
    }

    /**
     * 提交添加实验项目（含附件）
     * @param project 实验项目信息
     * @param files 上传附件
     */
    @RequiresPermissions("college:experimentProject:add")
    @PostMapping("/add/{arrangementId}")
    @ResponseBody
    public AjaxResult addSave(@PathVariable("arrangementId") Long arrangementId, ExperimentProject project, @RequestParam(value = "files", required = false) MultipartFile[] files) {
        return experimentProjectService.addExperimentProject(project, files);
    }

    /**
     * 跳转到修改实验项目页面
     * @param projectId 实验项目ID
     * @param mmap 传递项目数据
     */
    @RequiresPermissions("college:experimentProject:edit")
    @GetMapping("/edit/{projectId}")
    public String edit(@PathVariable("projectId") Long projectId, ModelMap mmap) {
        ExperimentProject project = experimentProjectService.getById(projectId);
        mmap.put("project", project);
        return prefix + "/edit";
    }

    /**
     * 提交修改实验项目（含附件）
     * @param project 实验项目信息
     * @param files 新上传附件
     */
    @RequiresPermissions("college:experimentProject:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ExperimentProject project, @RequestParam(value = "files", required = false) MultipartFile[] files) {
        return experimentProjectService.updateExperimentProject(project, files);
    }

    /**
     * 批量删除实验项目
     * @param projectIds 项目ID数组
     */
    @RequiresPermissions("college:experimentProject:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(@RequestParam("projectIds") Long[] projectIds) {
        return experimentProjectService.deleteProjectByIds(projectIds);
    }

    /**
     * 导出实验项目Excel
     * @param arrangementId 指定课程ID
     * @param response 响应流
     */
    @RequiresPermissions("college:experimentProject:export")
    @PostMapping("/export/{arrangementId}")
    public void export(@PathVariable("arrangementId") Integer arrangementId, HttpServletResponse response) {
        List<ExperimentProject> list = experimentProjectService.getProjectListByArrangementId(arrangementId);
        ExcelUtil<ExperimentProject> util = new ExcelUtil<>(ExperimentProject.class);
        util.exportExcel(response, list, "实验项目列表");
    }
}